# Create your views here.
from django.db.models import Q
from django.forms import model_to_dict
from rest_framework.views import APIView
from components.models import SysMenu, PackageManagement
from general.Transform import list_to_tree
from utils.views import Result
import json

class PackageManageUpdate(APIView):
    def get(self, request):
        data = request.query_params

        sys_menu = SysMenu.objects.filter(~Q(statu=0)).values()

        sys_menu_data = list(sys_menu)

        data = list_to_tree(sys_menu_data)

        return Result.success_response(data=data, msg='菜单加载成功')

    def post(self, request):

        try:
            data = request.data
            respData = {}
            rowId = data.get("id")
            PackageManagement.objects.filter(id=rowId).update(**data)
            respData = Result.success_response(data=[], msg='菜单加载成功')
        except Exception as e:
            respData = Result.fail_response(data=[], msg='执行失败')
            print(e)
        return respData
